﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Staticage.Data {
    public class ApplicationCache {
        private IDictionary<string, object> cache;
        private static ApplicationCache instance;

        private ApplicationCache() {
            this.cache = new Dictionary<string, object>();
        }

        public static ApplicationCache Instance {
            get {
                if (instance == null)
                    instance = new ApplicationCache();
                return instance;
            }
        }

        public ApplicationCache Set(string key, object value) {
            cache[key] = value;
            return this;
        }

        public object Get(string key) {
            return cache[key];
        }

        public T Get<T>(string key) {
            return (T)Get(key);
        }

        public ApplicationCache Remove(string key) {
            if (ContainsKey(key))
                cache.Remove(key);
            return this;
        }

        public ApplicationCache Clear() {
            cache.Clear();
            return this;
        }

        public bool ContainsKey(string key) {
            return cache.ContainsKey(key);
        }

        public bool TryGet(string key, out object value) {
            value = null;
            if (ContainsKey(key))
                value = Get(key);
            return ContainsKey(key) && value != null;
        }
    }
}
